/**
  ******************************************************************************
  * @file    DPC_DebugMode.c
  * @author  STMicroelectronics
  * @date    31-May-2022
  * @brief   This file contains functions definitions for Debug Mode usage
  ******************************************************************************
  *
  * @copyright (c) 2022(-2023) STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file in
  * the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  ******************************************************************************
  *
  */

/** @addtogroup STM32_DPOW
  * @brief  Digital Power application implemented with a STM32 MCU
  * @{
  */

/** @addtogroup Debug_Mode
  * @{
  */

/* Includes ------------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "app_X-CUBE-DPower.h"
#include "DPC_DebugMode.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#if defined (__ICCARM__)
#pragma location = ".ccmram" // IAR
#elif defined (__ARMCOMPILER_VERSION) || defined (__CC_ARM) || defined(__GNUC__)
 __attribute__( ( section ( ".ccmram" ) ) ) // CubeIDE and Keil
#endif
WAVEFORM_DISPLAY_INFO_t   Waveform = { 0 };


/* Private function prototypes -----------------------------------------------*/
/* External variables --------------------------------------------------------*/
/* External functions ---------------------------------------------------------*/
/* EWARM Waveform display functions */
/* ---------------------------------*/
/**
  * @brief Records user values
  * @details
  * This stored the user value in to a local buffer. Once all of the values are stored
  * we change state to playing the values back to the IDE
  */
void WaveFormRecord(int32_t ChA, int32_t ChB)
{
  switch (Waveform.m_State)
  {
  case WAVEFORM_WAIT_FOR_TRIGGER:
    break;

  case WAVEFORM_RECORD:
    Waveform.m_Buf[0][Waveform.m_IndexRec] = ChA;
    Waveform.m_Buf[1][Waveform.m_IndexRec] = ChB;
    if (++Waveform.m_IndexRec >= WAVEFORM_BUF_SIZE)
    {
      Waveform.m_State = WAVEFORM_PLAY;
      Waveform.m_IndexPlay = 0;
    }
    break;
  case WAVEFORM_PLAY:
    break;
  }
}

/**
  * @brief Waits for a trigger to start recording
  * @details
  * This waits until we detect a trigger then starts recording the user values.
  */
void WaveFormTrigger(void)
{
  switch (Waveform.m_State)
  {
  case WAVEFORM_WAIT_FOR_TRIGGER:
    Waveform.m_State = WAVEFORM_RECORD;
    Waveform.m_IndexRec = 0;
    break;

  case WAVEFORM_RECORD:
    if (Waveform.m_IndexRec > 80)
    {
      Waveform.m_State = WAVEFORM_PLAY;
      Waveform.m_IndexPlay = 0;
    }
    break;

  case WAVEFORM_PLAY:
    break;
  }
}

/**
  * @brief Display captured point using 'Set Data Breakpoint' 
  * @details
  * This re-plays the data recored on the IAR using the 'Set Data Breakpoint' feature.
  * When *pChA/B is written to the IAR detects the change and displays the value on the
  * ST-LINK TimeLine view. 
  */
void WaveFormDisplay(int32_t *pChA, int32_t *pChB)
{
  switch (Waveform.m_State)
  {
  case WAVEFORM_WAIT_FOR_TRIGGER:
    /*
    if (++Waveform.m_IndexTrig >= 60)
    {
      Waveform.m_State = WAVEFORM_RECORD;
      Waveform.m_IndexRec = 0;
    }
    */
    break;

  case WAVEFORM_RECORD:
    break;

  case WAVEFORM_PLAY:
    *pChA = Waveform.m_Buf[0][Waveform.m_IndexPlay];
    *pChB = Waveform.m_Buf[1][Waveform.m_IndexPlay];
    if (++Waveform.m_IndexPlay >= Waveform.m_IndexRec)
    {
      Waveform.m_State = WAVEFORM_WAIT_FOR_TRIGGER;
      Waveform.m_IndexTrig = 0;
    }
    break;
  }
}

/**
  * @} end Debug_Mode
  */

/**
  * @} end STM32_DPOW
  */
